package jp.sourceforge.akjrcp.text_douga.wizards.importimg;


import java.io.File;

import jp.sourceforge.akjrcp.commons.ui.FileDirChoicer;
import jp.sourceforge.akjrcp.commons.ui.AbstractFileDirChoicer.FileStatusChangeEvent;
import jp.sourceforge.akjrcp.commons.ui.AbstractFileDirChoicer.FileStatusChangeListener;
import jp.sourceforge.akjrcp.commons.ui.widget.LayoutDataBuilder;
import jp.sourceforge.akjrcp.commons.ui.widget.WidgetBuilder;
import jp.sourceforge.akjrcp.text_douga.parser.ParserUtils;
import jp.sourceforge.akjrcp.text_douga.rcp.Messages;
import jp.sourceforge.akjrcp.text_douga.rcp.TextDougaUtils;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class ImageInputPage  extends WizardPage{

	private CCombo size;
	private CCombo fps;
	private FileDirChoicer inputVideo;
	private CCombo duration;
	private CCombo wait;
	private CCombo fadein;
	private CCombo fadeout;
	private CCombo align;
	private CCombo valign;
	private CCombo loop;
	private Text timeLabelText;
	private Text imagesLabelText;

	protected ImageInputPage(String pageName) {
		super(pageName);
		setTitle(Messages.getString("importimg.input_title"));
		
		setDescription(Messages.getString("importimg.input_description"));
		setPageComplete(false);
	}

	public String getSize(){
		return size.getText();
	}
	public String getFps(){
		return fps.getText();
	}

	public String getPath(){
		return inputVideo.getAbsoluteFilePath();
	}

	private int imageIndex;
	public void createControl(Composite parent) {
		// TODO �����������ꂽ���\�b�h�E�X�^�u
		Composite base=new Composite(parent,SWT.NULL);
		base.setLayout(new GridLayout());
		setControl(base);
		
		inputVideo = new FileDirChoicer(base,SWT.NULL,FileDirChoicer.DIR,Messages.getString("importimg.input"));
		inputVideo.setLayoutData(LayoutDataBuilder.fillHorizontalGridData());
		inputVideo.setFilterExtensions(ParserUtils.toMixFilterExtensions(ParserUtils.videos));
		inputVideo.addFileListChangeListener(new FileStatusChangeListener(){
			
			

			@Override
			public void addList(FileStatusChangeEvent event) {
				setPageComplete(false);
				File dir=new File(event.getFilePath());
				String list[]=dir.list();
				imageIndex = 0;
				for (int i = 0; i < list.length; i++) {
					if(ParserUtils.isImageFile(list[i])){
						imageIndex++;
					}
				}
				imagesLabelText.setText(""+imageIndex);
				calucurateTime();
				setPageComplete(true);
				
			}

			@Override
			public void removeList(FileStatusChangeEvent event) {
				size.setText("");
				fps.setText("");
			}});
		
		Composite buttons=WidgetBuilder.createGridLayoutComposite(base, SWT.NULL,4,true);
		
		Button speedSlide=new Button(buttons,SWT.NULL);
		speedSlide.setText(Messages.getString("importimg.speedSlide"));
		speedSlide.addSelectionListener(new SelectionListener(){
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				setSpeedValue();
				calucurateTime();
			}});
		Button simpleSlide=new Button(buttons,SWT.NULL);
		simpleSlide.setText(Messages.getString("importimg.simpleSlide"));
		simpleSlide.addSelectionListener(new SelectionListener(){
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				setSimpleValue();
				calucurateTime();
			}});
		Button flashSlide=new Button(buttons,SWT.NULL);
		flashSlide.setText(Messages.getString("importimg.flashSlide"));
		flashSlide.addSelectionListener(new SelectionListener(){
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				setFlashValue();
				calucurateTime();
			}});
		Button videoSlide=new Button(buttons,SWT.NULL);
		videoSlide.setText(Messages.getString("importimg.videoSlide"));
		videoSlide.addSelectionListener(new SelectionListener(){
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				setVideoValue();
				calucurateTime();
			}});
		
		
		Composite labels=WidgetBuilder.createGridLayoutComposite(base, SWT.NULL,4,true);
		
		Label label=new Label(labels,SWT.NULL);
		label.setText(Messages.getString("importimg.timelabel"));
		timeLabelText = new Text(labels,SWT.READ_ONLY);
		//timeLabelText.setText("53");
		timeLabelText.setLayoutData(LayoutDataBuilder.fillHorizontalGridData());
		
		Label label2=new Label(labels,SWT.NULL);
		label2.setText(Messages.getString("importimg.imagelabel"));
		imagesLabelText = new Text(labels,SWT.READ_ONLY);
		//timeLabelText.setText("53");
		imagesLabelText.setLayoutData(LayoutDataBuilder.fillHorizontalGridData());
		Composite fields=WidgetBuilder.createTwoGridLayoutComposite(base, SWT.NULL);
		
		
		SelectionListener doCaricurate=new SelectionListener(){

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				calucurateTime();
			}};
		duration=createCCombo(fields,Messages.getString("importimg.duration"),new String[]{"","0.1","0.3","1","2","3","5","10","30","0.0334"},SWT.NULL);
		duration.addSelectionListener(doCaricurate);
		wait=createCCombo(fields,Messages.getString("importimg.wait"),new String[]{"","0.1","0.3","1","3","5","10"},SWT.NULL);
		wait.addSelectionListener(doCaricurate);
		
		fadein=createCCombo(fields,Messages.getString("importimg.fadein"),new String[]{"","0.1","0.3","1","3","5","10"},SWT.NULL);
		
		
		fadeout=createCCombo(fields,Messages.getString("importimg.fadeout"),new String[]{"","0.1","0.3","1","3","5","10"},SWT.NULL);
		
		
	
		size=createCCombo(fields,Messages.getString("exportimg.size"),new String[]{"","160x120","320x240","640x480","720x480"},SWT.NULL);
		int w_h[]=TextDougaUtils.getDefaultSize();
		size.setText(""+w_h[0]+"x"+w_h[1]);
		
		
		fps=createCCombo(fields,Messages.getString("exportimg.fps"),new String[]{"","1","5","10","12","14.895","15","24","25","29.97","30"},SWT.NULL);
		fps.setText("5");//for slide show
		//fps.setText(""+TextDougaUtils.getDefaultFPS());
		
		align=createCCombo(fields,Messages.getString("importimg.align"),new String[]{"","left","center","right"},SWT.READ_ONLY);
		valign=createCCombo(fields,Messages.getString("importimg.valign"),new String[]{"","top","middle","bottom"},SWT.READ_ONLY);
		
		loop=createCCombo(fields,Messages.getString("importimg.loop"),new String[]{"","1","3","5","10","99"},SWT.NULL);
		loop.addSelectionListener(doCaricurate);
		//transparent=createCCombo(fields,Messages.getString("exportimg.valign"),new String[]{"","top","middle","bottom"},SWT.READ_ONLY);
		
		setSpeedValue();
	}
	
	public double toDouble(String num,double defaultValue){
		if(num==null){
			return defaultValue;
		}
		double ret=defaultValue;
		try{
			ret=Double.parseDouble(num);
		}catch(Exception e){};
		System.out.println(ret);
		return ret;
	}
	protected void calucurateTime() {
		double max=0;
		if(imageIndex>0){

			max=(imageIndex*toDouble(duration.getText(),1)) + ((imageIndex-1)*toDouble(wait.getText(),0.1));
			max*=toDouble(loop.getText(),1);
		}
		
		timeLabelText.setText(""+(int)max+" "+Messages.getString("importimg.sec"));
	}

	private void setSimpleValue(){
		fadein.setText("0");
		fadeout.setText("0");
		wait.setText("0");
		duration.setText("2");
		fps.setText("5");
	}
	
	private void setSpeedValue(){
		fadein.setText("0");
		fadeout.setText("0");
		wait.setText("0");
		duration.setText("0.1");
		fps.setText("5");
	}
	
	private void setFlashValue(){
		fadein.setText("0.2");
		fadeout.setText("0.2");
		wait.setText("0.1");
		duration.setText("3");
		fps.setText("10");
	}
	
	private void setVideoValue(){
		fadein.setText("0");
		fadeout.setText("0");
		wait.setText("0");
		duration.setText("0.0334");
		fps.setText("29.97");
	}
	
	private CCombo createCCombo(Composite parent,String label,String values[],int style){
	
		Composite p2=WidgetBuilder.createTwoGridLayoutComposite(parent, SWT.NULL);
		WidgetBuilder.createLabel(p2, label);
		CCombo combo=new CCombo(p2,style);
		combo.setLayoutData(LayoutDataBuilder.hintGridData(200, 0));
		
		for (int i = 0; i < values.length; i++) {
			combo.add(values[i]);
		}
		return combo;
	}
	
	private void addSizeValue(String value){
		String item[]=size.getItems();
		boolean match=false;
		for (int i = 0; i < item.length; i++) {
			if(item[i].equals(value)){
				match=true;
				break;
			}
		}
		
		if(!match){
			size.add(value);
		}
	}

	public String getDuration() {
		return duration.getText();
	}



	public String getWait() {
		return wait.getText();
	}



	public String getFadein() {
		return fadein.getText();
	}


	public String getFadeout() {
		return fadeout.getText();
	}


	public String getValign() {
		return valign.getText();
	}


	public String getLoop() {
		return loop.getText();
	}


	public String getAlign() {
		return align.getText();
	}


	

}
